<?php

namespace api\modules\v1\forms\buss;

use common\helpers\DateHelper;
use common\helpers\ValidatorHelper;
use common\models\base\Project;
use common\models\buss\Accounts;
use common\models\buss\Content;
use common\models\buss\ContentPublishRecord;
use common\models\buss\Doctors;
use services\member\MemberService;
use Yii;
use yii\base\Model;


class ContentPublishRecordForm extends Model
{
    /**
     * @var int $id ID
     * @var int $content_id 内容ID
     * @var int $doctor_id 医生ID
     * @var array $account_ids 账号IDs
     * @var string $publish_time 发布日期【YYYY-MM-DD HH:II】
     * @var int $read_num 浏览量
     * @var int $like_num 点赞
     * @var int $collect_num 收藏
     * @var int $comment_num 评论
     */
    public $id;
    public $content_id;
    public $doctor_id;
    public $account_ids;
    public $publish_time;
    public $read_num;
    public $like_num;
    public $collect_num;
    public $comment_num;

    /**
     * @var Content $_content
     * @var ContentPublishRecord $_contentPublishRecord
     * @var Doctors $_doctor
     * @var Accounts $_account
     * @var Project[] $_haveProjectList
     */
    protected $_content;
    protected $_contentPublishRecord;
    protected $_doctor;// 医生
    protected $_accountList;// 账号
    protected $_haveProjectList;// 权限下项目列表


    public function rules()
    {
        return [
            // 参数验证
            [['project_id', 'type', 'content', 'c_id', 'is_stream', 'publish_status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],
            [['id', 'project_id', 'type', 'content', 'c_id', 'is_stream', 'publish_status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],
            [['id'], 'required', 'message' => '{attribute}不能为空', 'on' => ['delete']],

            [['id'], 'checkId', 'skipOnEmpty' => false, 'on' => ['edit_data', 'delete']],
            [['content_id'], 'checkContentId', 'skipOnEmpty' => false, 'on' => ['create']],
            [['doctor_id'], 'checkDoctorId', 'skipOnEmpty' => false, 'on' => ['create']],
            [['account_ids'], 'checkAccountsId', 'skipOnEmpty' => false, 'on' => ['create']],
            [['publish_time'], 'checkPublishTime', 'skipOnEmpty' => false, 'on' => ['create']],
            [['read_num'], 'checkReadNum', 'skipOnEmpty' => false, 'on' => ['edit_data']],
            [['like_num'], 'checkLikeNum', 'skipOnEmpty' => false, 'on' => ['edit_data']],
            [['collect_num'], 'checkCollectNum', 'skipOnEmpty' => false, 'on' => ['edit_data']],
            [['comment_num'], 'checkCommentNum', 'skipOnEmpty' => false, 'on' => ['edit_data']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 发布
        $scenarios['create'] = ['content_id', 'doctor_id', 'account_ids', 'publish_time'];

        // 更新数据
        $scenarios['edit_data'] = ['id', 'read_num', 'like_num', 'collect_num', 'comment_num'];

        // 删除
        $scenarios['delete'] = ['id'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'content_id' => '内容ID',
            'doctor_id' => '医生',
            'account_ids' => '账号',
            'publish_time' => '发布日期',
            'read_num' => '浏览量',
            'like_num' => '点赞数',
            'collect_num' => '收藏数',
            'comment_num' => '评论数',
        ];
    }

    /**
     * 检验ID
     * @param $attribute
     * @return void
     */
    public function checkId($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $contentPublishRecord = $this->getContentPublishRecord();
        if (!$contentPublishRecord) {
            $this->addError($attribute, '发布内容不存在');
            return;
        }
        if ($contentPublishRecord->status == ContentPublishRecord::STATUS_DELETE) {
            $this->addError($attribute, '发布内容已删除');
            return;
        }
    }

    /**
     * 检验内容ID
     * @param $attribute
     * @return void
     */
    public function checkContentId($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $content = $this->getContent();
        if (!$content) {
            $this->addError($attribute, '内容不存在');
            return;
        }

        if ($content->status == Content::STATUS_DELETE) {
            $this->addError($attribute, '内容已删除');
            return;
        }
    }


    /**
     * 校验医生ID
     * @param $attribute
     * @return void
     */
    public function checkDoctorId($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $doctor = $this->getDoctor();
        if (empty($doctor)) {
            $this->addError($attribute, '医生不存在');
            return;
        }
        if ($doctor->status != Doctors::STATUS_ENABLE) {
            $this->addError($attribute, '医生状态异常');
            return;
        }
        if (in_array($doctor->project_id, array_column($this->getHaveProjectList(), 'value')) === false) {
            $this->addError($attribute, '无权限选择该项目医生');
            return;
        }
    }

    /**
     * 校验账号ID
     * @param $attribute
     * @return void
     */
    public function checkAccountsId($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $accounts = $this->getAccountList();
        if (empty($accounts)) {
            $this->addError($attribute, '账号不存在');
            return;
        }
        foreach ($accounts as $account) {
            if ($account->status != Doctors::STATUS_ENABLE) {
                $this->addError($attribute, '账号状态异常');
                return;
            }
            if (in_array($account->project_id, array_column($this->getHaveProjectList(), 'value')) === false) {
                $this->addError($attribute, '无权限选择该项目账号');
                return;
            }
        }
    }

    /**
     * 校验发布时间
     * @param $attribute
     * @return void
     */
    public function checkPublishTime($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (ValidatorHelper::validateDate($this->publish_time) === false) {
            $this->addError($attribute, '发布日期格式错误【YYYY-MM-DD HH:II】');
            return;
        }
        $today = DateHelper::today();
        if (strtotime($this->publish_time) > $today['end']) {
            $this->addError($attribute, '发布时间不能超过当天');
            return;
        }
    }

    /**
     * 校验浏览量
     * @param $attribute
     * @return void
     */
    public function checkReadNum($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->read_num) && is_numeric($this->read_num) === false) {
            return;
        }
        if ($this->read_num < 0) {
            $this->addError($attribute, '浏览量不能为负数');
            return;
        }
    }

    /**
     * 校验点赞量
     * @param $attribute
     * @return void
     */
    public function checkLikeNum($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->like_num) && is_numeric($this->like_num) === false) {
            return;
        }
        if ($this->like_num < 0) {
            $this->addError($attribute, '点赞数不能为负数');
            return;
        }
    }

    /**
     * 校验收藏数
     * @param $attribute
     * @return void
     */
    public function checkCollectNum($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->collect_num) && is_numeric($this->collect_num) === false) {
            return;
        }
        if ($this->collect_num < 0) {
            $this->addError($attribute, '收藏数不能为负数');
            return;
        }
    }

    /**
     * 校验评论数
     * @param $attribute
     * @return void
     */
    public function checkCommentNum($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->comment_num) && is_numeric($this->comment_num) === false) {
            return;
        }
        if ($this->comment_num < 0) {
            $this->addError($attribute, '评论数不能为负数');
            return;
        }
    }

    /**
     * 获取内容信息
     * @return Content|null
     */
    public function getContent(): ?Content
    {
        if (empty($this->_content)) {
            $this->_content = Content::findOne($this->content_id);
        }

        return $this->_content;
    }

    /**
     * 获取内容信息
     * @return ContentPublishRecord|null
     */
    public function getContentPublishRecord(): ?ContentPublishRecord
    {
        if (empty($this->_contentPublishRecord)) {
            $this->_contentPublishRecord = ContentPublishRecord::findOne($this->id);
        }

        return $this->_contentPublishRecord;
    }

    /**
     * 获取医生信息
     * @return Doctors|null
     */
    public function getDoctor(): ?Doctors
    {
        if (empty($this->_doctor)) {
            $this->_doctor = Doctors::findOne($this->doctor_id);
        }

        return $this->_doctor;
    }

    /**
     * 获取账号信息
     * @return Accounts[]
     */
    public function getAccountList(): array
    {
        if (is_null($this->_accountList)) {
            $this->_accountList = Accounts::findAll(['id' => $this->account_ids]);
        }

        return $this->_accountList;
    }

    /**
     * 获取账号信息
     * @return Project[]
     */
    public function getHaveProjectList(): array
    {
        if (empty($this->_haveProjectList)) {
            $this->_haveProjectList = MemberService::getProjectEditCondition();
        }

        return $this->_haveProjectList;
    }
}
